package form

import (
	"yunj/app/controller/admin/demo"
	formService "yunj/app/service/admin/demo/form"
	"yunj/pkg/yunj/core/builder"
	"yunj/pkg/yunj/core/builder/builderdto"
	"yunj/pkg/yunj/core/builder/form"
	"yunj/pkg/yunj/core/builder/form/button"
	"yunj/pkg/yunj/core/response"

	"github.com/gin-gonic/gin"
)

type Index struct {
	demo.Controller
}

// 示例
func (ctrl *Index) Index(ctx *gin.Context) {
	formBuilder := builder.NewForm(ctx, "demo").
		Tab([]builderdto.FormTab{{Code: "normal", Title: "常规"}, {Code: "required", Title: "必填"}, {Code: "default", Title: "默认"}, {Code: "readonly", Title: "只读"}}).
		Field(func(f *builder.Form, tab *builderdto.FormTab) (fields []form.FieldInterface, err error) {
			fields, err = CommonFields(ctx, tab)
			return
		}).
		// Button([]form.ButtonInterface{&button.Reload{}, &button.Reset{}, &button.Submit{}}).
		Button([]form.ButtonInterface{button.NewReload(), button.NewReset(), button.NewSubmit()}).
		Load(func(f *builder.Form) (any, error) {
			return map[string]any{"default_text123": "你好啊"}, nil
		}).
		Submit(func(f *builder.Form, values map[string]any) (res builderdto.FormSubmitResult, err error) {
			return
		})

	// qwe := -1
	// err := global.Validate.Var(qwe, "positiveInt")
	// if err != nil {
	// 	for _, e := range err.(validator.ValidationErrors) {
	// 		fmt.Println(666, e.Translate(global.ValidateTrans))
	// 	}
	// }

	ctrl.RenderForm(ctx, formBuilder)
}

// 下拉搜索可选项
func (ctrl *Index) DropdownSearchOptions(ctx *gin.Context) {
	options, err := formService.DropdownSearchOptions(ctx)
	if err != nil {
		response.Fail(ctx, err.Error())
	} else {
		response.Success(ctx, options)
	}
}
